package com.orderSystem;

import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
import java.text.*;

import org.apache.commons.lang.ObjectUtils.Null;
import com.databaseManager.*;
import com.mysql.fabric.Response;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import net.sf.json.JSONObject;


public class Handler {

    // test
    static class TestHandler implements HttpHandler {

	@Override
	public void handle(HttpExchange arg0) throws IOException {
	    new Thread(new Runnable() {

		@Override
		public void run() {
		    try {
			String queryString = arg0.getRequestURI().getQuery();
			Map<String, String> queryStringInfo = FormData2Dic.formData2Dic(queryString);
			arg0.sendResponseHeaders(200, 0);
			OutputStream os = arg0.getResponseBody();
			Map<String, String> responseString = new HashMap<>();
			/* 如果搜索到结果，且密码正确，返回1，否则0 */
			responseString.put("1", "2");
			JSONObject jsonObject = JSONObject.fromObject(responseString);
			String response = jsonObject.toString();
			os.write(response.getBytes());
			os.close();
		    } catch (IOException ie) {
			ie.printStackTrace();
			// TODO Auto-generated catch block
		    } catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		    }
		    // TODO Auto-generated method stub
		}
	    }).start();
	    // TODO Auto-generated method stub
	}
    }

	//1、病人注册
    static class RegisterHandler implements HttpHandler {

	@Override
	public void handle(HttpExchange arg0) throws IOException {
	    new Thread(new Runnable() {

		@Override
		public void run() {
		    try {
			DatabaseFunc df = new DatabaseFunc();
			String queryString = arg0.getRequestURI().getQuery();
			System.out.println(queryString);
			Map<String, String> queryStringInfo = FormData2Dic.formData2Dic(queryString);
			arg0.sendResponseHeaders(200, 0);
			Map<String, String> responseString = new HashMap<>();
			try
			{
				df.addPatient(queryStringInfo.get("usrID"),
						  queryStringInfo.get("usrPwd"),
						  queryStringInfo.get("usrRealName"),
						  Integer.parseInt(queryStringInfo.get("usrAge")),
						  queryStringInfo.get("usrGen"),
						  null,
						  null,
						  null);
				responseString.put("stat","1");
			}catch (Exception e){
				responseString.put("stat","0");
			}
			JSONObject jsonObject = JSONObject.fromObject(responseString);
			String response = jsonObject.toString();
			OutputStream os = arg0.getResponseBody();
			os.write(response.getBytes());
			os.close();
		    } catch (IOException ie) {
			ie.printStackTrace();
			// TODO Auto-generated catch block
		    } catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		    }
		    // TODO Auto-generated method stub
		}
	    }).start();
	    // TODO Auto-generated method stub
	}
    }

    //2、病人登录
    static class PatientLoginHandler implements HttpHandler {

	@Override
	public void handle(HttpExchange arg0) throws IOException {
	    new Thread(new Runnable() {

		@Override
		public void run() {
		    try {
			DatabaseFunc df = new DatabaseFunc();
			String queryString = arg0.getRequestURI().getQuery();
			System.out.println(queryString);
			/* 将获取到的报文体转换为键值对 */
			Map<String, String> queryStringInfo = FormData2Dic.formData2Dic(queryString);
			arg0.sendResponseHeaders(200, 0);
			Patient patient = df.findPatient(queryStringInfo.get("usrID"));
			Map<String, String> responseString = new HashMap<>();
			/* 如果搜索到结果，且密码正确，返回1，否则0 */
			if (patient != null && (queryStringInfo.get("usrPwd").equals(patient.getPassword())))
			    responseString.put("stat", "1");
			else
			    responseString.put("stat", "0");
			/* 转换为json对象 */
			JSONObject jsonObject = JSONObject.fromObject(responseString);
			OutputStream os = arg0.getResponseBody();
			String response = jsonObject.toString();
			System.out.println(response);
			os.write(response.getBytes());
			os.close();
		    } catch (IOException ie) {
			ie.printStackTrace();
			// TODO Auto-generated catch block
		    } catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		    }
		    // TODO Auto-generated method stub
		}
	    }).start();
	    // TODO Auto-generated method stub
	}
    }

    //3、医生登录
    static class DoctorLoginHandler implements HttpHandler {

	@Override
	public void handle(HttpExchange arg0) throws IOException {
	    new Thread(new Runnable() {

		@Override
		public void run() {
		    try {
			DatabaseFunc df = new DatabaseFunc();
			System.out.println("ok");
			String queryString = arg0.getRequestURI().getQuery();
			/* 将获取到的报文体转换为键值对 */
			Map<String, String> queryStringInfo = FormData2Dic.formData2Dic(queryString);
			arg0.sendResponseHeaders(200, 0);
			Doctor doctor = df.findDoctor(queryStringInfo.get("usrID"));
			Map<String, String> responseString = new HashMap<>();
			/* 如果搜索到结果，且密码正确，返回1，否则0 */
			if (doctor != null && queryStringInfo.get("usrPwd") == doctor.getPassword())
			    responseString.put("stat", "1");
			else
			    responseString.put("stat", "0");
			/* 转换为json对象 */
			JSONObject jsonObject = JSONObject.fromObject(responseString);
			OutputStream os = arg0.getResponseBody();
			String response = jsonObject.toString();
			os.write(response.getBytes());
			os.close();
		    } catch (IOException ie) {
			ie.printStackTrace();
			// TODO Auto-generated catch block
		    } catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		    }
		    // TODO Auto-generated method stub
		}
	    }).start();
	    // TODO Auto-generated method stub
	}
    }

    //4、专家预约
    static class ProOrderHandler implements HttpHandler {

	@Override
	public void handle(HttpExchange arg0) throws IOException {
	    new Thread(new Runnable() {

		@Override
		public void run() {
		    try {
			Random random = new Random();
			QueueHandle queueHandle = new QueueHandle();
			DatabaseFunc df = new DatabaseFunc();
			String queryString = arg0.getRequestURI().getQuery();
			System.out.println(queryString);
			/* 将获取到的报文体转换为键值对 */
			Map<String, String> queryStringInfo = FormData2Dic.formData2Dic(queryString);
			arg0.sendResponseHeaders(200, 0);
			Patient patient = df.findPatient(queryStringInfo.get("usrID"));
			String stat = queueHandle.addproQueue(patient);
			Map<String, String> responseString = new HashMap<>();

			if (stat == "1")
			    responseString.put("stat", "1");
			else if (stat == "2")
			    responseString.put("stat", "2");
			else
			    responseString.put("stat", "0");

			int size = df.searchProfessorByDepName(queryStringInfo.get("subject")).size();
			//医生
			Doctor professor = df.searchProfessorByDepName(queryStringInfo.get("subject")).get(random.nextInt(size));
			//TODO:与数据库通信获取专家名字与预约号
			int orderNum = random.nextInt(1000);
			ProOrder proOrder = new ProOrder(orderNum,queryStringInfo.get("usrID"),
											queryStringInfo.get("date")+" "+queryStringInfo.get("time"),
											 professor.getUid());
			df.insertProOrder(proOrder);
			responseString.put("proName",professor.getName());
			responseString.put("orderNum", String.valueOf(orderNum));
			/* 转换为json对象 */
			JSONObject jsonObject = JSONObject.fromObject(responseString);
			OutputStream os = arg0.getResponseBody();
			String response = jsonObject.toString();
			os.write(response.getBytes());
			os.close();
		    } catch (IOException ie) {
			ie.printStackTrace();
			// TODO Auto-generated catch block
		    } catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		    }
		    // TODO Auto-generated method stub
		}
	    }).start();
	    // TODO Auto-generated method stub
	}
    }

    //5、科室预约
    static class DepOrderHandler implements HttpHandler {

	@Override
	public void handle(HttpExchange arg0) throws IOException {
	    new Thread(new Runnable() {

		@Override
		public void run() {
		    try {
		    Random random = new Random();
			QueueHandle queueHandle = new QueueHandle();
			DatabaseFunc df = new DatabaseFunc();
			String queryString = arg0.getRequestURI().getQuery();
			/* 将获取到的报文体转换为键值对 */
			Map<String, String> queryStringInfo = FormData2Dic.formData2Dic(queryString);
			arg0.sendResponseHeaders(200, 0);
			Patient patient = df.findPatient(queryStringInfo.get("usrID"));
			/*获取科室预约号*/
			int orderNum = random.nextInt(9999);
            Map<String, String> responseString = new HashMap<>();
			responseString.put("orderNum", String.valueOf(orderNum));
			String stat = queueHandle.adddepQueue(patient);
			if (stat == "1")
			    responseString.put("stat", "1");
			else
			    responseString.put("stat", "0");
			/* 转换为json对象 */
			DepOrder depOrder = new DepOrder(orderNum,
											 patient.getUid(),
											queryStringInfo.get("date")+" "+queryStringInfo.get("time"),
											 df.findDepartment(queryStringInfo.get("subject"),SqlOP.GETDEPNO));
			df.insertDepOrder(depOrder);
			JSONObject jsonObject = JSONObject.fromObject(responseString);
			OutputStream os = arg0.getResponseBody();
			String response = jsonObject.toString();
			os.write(response.getBytes());
			os.close();
		    } catch (IOException ie) {
			ie.printStackTrace();
			// TODO Auto-generated catch block
		    } catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		    }
		    // TODO Auto-generated method stub
		}
	    }).start();
	    // TODO Auto-generated method stub
	}
    }

    //6、现场科室挂号
    static class DepLiveHandler implements HttpHandler {

	@Override
	public void handle(HttpExchange arg0) throws IOException {
	    new Thread(new Runnable() {

		@Override
		public void run() {
		    try {
		    Random random = new Random();
			QueueHandle queueHandle = new QueueHandle();
			DatabaseFunc df = new DatabaseFunc();
			String queryString = arg0.getRequestURI().getQuery();
			/* 将获取到的报文体转换为键值对 */
			Map<String, String> queryStringInfo = FormData2Dic.formData2Dic(queryString);
			arg0.sendResponseHeaders(200, 0);
			// TODO：存储预约信息
			Patient patient = df.findPatient(queryStringInfo.get("usrID"));
			Map<String, String> responseString = new HashMap<>();
			responseString.put("stat", "1");
			long nowDate = new Date().getTime();
			String sid = Integer.toHexString((int)nowDate);
			responseString.put("liveNum", sid);
			//根据当前时间戳生成随机数
			df.insertLiveNum(Long.parseLong(sid),
							queryStringInfo.get("usrID"),
							df.findDepartment("depName",SqlOP.GETDEPNAME),
							SqlOP.GETDEPNAME);
			/* 转换为json对象 */
			JSONObject jsonObject = JSONObject.fromObject(responseString);
			OutputStream os = arg0.getResponseBody();
			String response = jsonObject.toString();
			os.write(response.getBytes());
			os.close();
		    } catch (IOException ie) {
			ie.printStackTrace();
			// TODO Auto-generated catch block
		    } catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		    }
		    // TODO Auto-generated method stub
		}
	    }).start();
	    // TODO Auto-generated method stub
	}
    }

    //7、现场专家挂号
	static class ProLiveHandler implements HttpHandler {
		@Override
		public void handle(HttpExchange arg0) throws IOException {
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						Random random = new Random();
						DatabaseFunc df = new DatabaseFunc();
						String queryString = arg0.getRequestURI().getQuery();
						/* 将获取到的报文体转换为键值对 */
						Map<String, String> queryStringInfo = FormData2Dic.formData2Dic(queryString);
						arg0.sendResponseHeaders(200, 0);
						// TODO：存储预约信息
						Map<String, String> responseString = new HashMap<>();
						responseString.put("stat", "1");
						//根据当前时间戳生成随机数
						long nowDate = new Date().getTime();
						String sid = Integer.toHexString((int)nowDate);
						responseString.put("liveNum", sid);
						int size = df.searchProfessorByDepName(queryStringInfo.get("subject")).size();
						df.insertLiveNum(Long.parseLong(sid),
								queryStringInfo.get("usrID"),
								df.searchProfessorByDepName(queryStringInfo.get("subject")).get(random.nextInt(size)).getRNo(),
								SqlOP.GETDEPNAME);
						/* 转换为json对象 */
						JSONObject jsonObject = JSONObject.fromObject(responseString);
						OutputStream os = arg0.getResponseBody();
						String response = jsonObject.toString();
						os.write(response.getBytes());
						os.close();
					} catch (IOException ie) {
						ie.printStackTrace();
						// TODO Auto-generated catch block
					} catch (Exception e) {
						e.printStackTrace();
						// TODO: handle exception
					}
					// TODO Auto-generated method stub
				}
			}).start();
			// TODO Auto-generated method stub
		}
	}

	//8、预约科室分诊
	static class DepTriageHandler implements HttpHandler{
		@Override
		public void handle(HttpExchange arg0) throws IOException {
			new Thread(new Runnable() {

				@Override
				public void run() {
					try {
						QueueHandle queueHandle = new QueueHandle();
						DatabaseFunc df = new DatabaseFunc();
						String queryString = arg0.getRequestURI().getQuery();
						/* 将获取到的报文体转换为键值对 */
						Map<String, String> queryStringInfo = FormData2Dic.formData2Dic(queryString);
						arg0.sendResponseHeaders(200, 0);
						// TODO：存储预约信息
						Map<String, String> responseString = new HashMap<>();
						Patient patient = df.findPatient(queryStringInfo.get("usrID"));
						String stat = queueHandle.addpriorityDepQueue(patient);
						if (stat == "1")
							responseString.put("stat","1");
						else
							responseString.put("stat","0");
						queueHandle.removedepList(patient);
						//根据当前时间戳生成随机数
						long nowDate = new Date().getTime();
						String sid = Integer.toHexString((int)nowDate);
						Date dNow = new Date( );
						SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
						responseString.put("priorityQueueNum", sid);
						queueHandle.inName_priorityDepQueue(patient,sid);
						QueueNumber queueNumber = new QueueNumber(Long.parseLong(sid),queryStringInfo.get("usrID"),ft.format(dNow));
						String subject = queueNumber.getDepName();
						responseString.put("subject",subject);
						/* 转换为json对象 */
						JSONObject jsonObject = JSONObject.fromObject(responseString);
						OutputStream os = arg0.getResponseBody();
						String response = jsonObject.toString();
						os.write(response.getBytes());
						os.close();
					} catch (IOException ie) {
						ie.printStackTrace();
						// TODO Auto-generated catch block
					} catch (Exception e) {
						e.printStackTrace();
						// TODO: handle exception
					}
					// TODO Auto-generated method stub
				}
			}).start();
			// TODO Auto-generated method stub
		}
	}

	//9、预约专家分诊
	static class ProTriageHandler implements HttpHandler{
		@Override
		public void handle(HttpExchange arg0) throws IOException {
			new Thread(new Runnable() {

				@Override
				public void run() {
					try {
						QueueHandle queueHandle = new QueueHandle();
						DatabaseFunc df = new DatabaseFunc();
						String queryString = arg0.getRequestURI().getQuery();
						/* 将获取到的报文体转换为键值对 */
						Map<String, String> queryStringInfo = FormData2Dic.formData2Dic(queryString);
						Patient patient = df.findPatient(queryStringInfo.get("usrID"));
						arg0.sendResponseHeaders(200, 0);
						Map<String, String> responseString = new HashMap<>();
						queueHandle.removeproList(patient);
						String stat = queueHandle.addpriorityProQueue(patient);

						if (stat == "1")
							responseString.put("stat","1");
						else
							responseString.put("stat","0");

						long nowDate = new Date().getTime();
						String sid = Integer.toHexString((int)nowDate);
						Date dNow = new Date( );
						SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
						responseString.put("priorityQueueNum", sid);
						queueHandle.inName_priorityProQueue(patient,sid);
						QueueNumber queueNumber = new QueueNumber(Long.parseLong(sid),queryStringInfo.get("usrID"),ft.format(dNow));
						if(queueNumber.isProQueue())
						{
							responseString.put("proName",df.findDoctor(queueNumber.getEid()).getName());
							responseString.put("subject",queueNumber.getDepName());
						}

						/* 转换为json对象 */
						JSONObject jsonObject = JSONObject.fromObject(responseString);
						OutputStream os = arg0.getResponseBody();
						String response = jsonObject.toString();
						os.write(response.getBytes());
						os.close();
					} catch (IOException ie) {
						ie.printStackTrace();
						// TODO Auto-generated catch block
					} catch (Exception e) {
						e.printStackTrace();
						// TODO: handle exception
					}
					// TODO Auto-generated method stub
				}
			}).start();
			// TODO Auto-generated method stub
		}
	}

	//10、现场科室分诊
	static class DepLiveTriageHandler implements HttpHandler{
		@Override
		public void handle(HttpExchange arg0) throws IOException {
			new Thread(new Runnable() {

				@Override
				public void run() {
					try {
						QueueHandle queueHandle = new QueueHandle();
						DatabaseFunc df = new DatabaseFunc();
						String queryString = arg0.getRequestURI().getQuery();
						/* 将获取到的报文体转换为键值对 */
						Map<String, String> queryStringInfo = FormData2Dic.formData2Dic(queryString);
						arg0.sendResponseHeaders(200, 0);
						// TODO：存储预约信息
						Map<String, String> responseString = new HashMap<>();
						Patient patient = df.findPatient(queryStringInfo.get("usrID"));
						String stat = queueHandle.addnormalDepQueue(patient);
						if (stat == "1")
							responseString.put("stat","1");
						else
							responseString.put("stat","0");
						//根据当前时间戳生成随机数
						long nowDate = new Date().getTime();
						String sid = Integer.toHexString((int)nowDate);
						Date dNow = new Date( );
						SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
						responseString.put("priorityQueueNum", sid);
						queueHandle.inName_normalDepQueue(patient,sid);
						QueueNumber queueNumber = new QueueNumber(Long.parseLong(sid),queryStringInfo.get("usrID"),ft.format(dNow));
						String subject = queueNumber.getDepName();
						responseString.put("subject",subject);
						/* 转换为json对象 */
						JSONObject jsonObject = JSONObject.fromObject(responseString);
						OutputStream os = arg0.getResponseBody();
						String response = jsonObject.toString();
						os.write(response.getBytes());
						os.close();
					} catch (IOException ie) {
						ie.printStackTrace();
						// TODO Auto-generated catch block
					} catch (Exception e) {
						e.printStackTrace();
						// TODO: handle exception
					}
					// TODO Auto-generated method stub
				}
			}).start();
			// TODO Auto-generated method stub
		}



	}

	//11、现场专家分诊
	static class ProLiveTriageHandler implements HttpHandler{
		@Override
		public void handle(HttpExchange arg0) throws IOException {
			new Thread(new Runnable() {

				@Override
				public void run() {
					try {
						QueueHandle queueHandle = new QueueHandle();
						DatabaseFunc df = new DatabaseFunc();
						String queryString = arg0.getRequestURI().getQuery();
						/* 将获取到的报文体转换为键值对 */
						Map<String, String> queryStringInfo = FormData2Dic.formData2Dic(queryString);
						Patient patient = df.findPatient(queryStringInfo.get("usrID"));
						arg0.sendResponseHeaders(200, 0);
						Map<String, String> responseString = new HashMap<>();
						String stat = queueHandle.addnormalDepQueue(patient);

						if (stat == "1")
							responseString.put("stat","1");
						else
							responseString.put("stat","0");

						long nowDate = new Date().getTime();
						String sid = Integer.toHexString((int)nowDate);
						Date dNow = new Date( );
						SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
						responseString.put("priorityQueueNum", sid);
						queueHandle.inName_normalProQueue(patient,sid);
						QueueNumber queueNumber = new QueueNumber(Long.parseLong(sid),queryStringInfo.get("usrID"),ft.format(dNow));
						if(queueNumber.isProQueue())
						{
							responseString.put("proName",df.findDoctor(queueNumber.getEid()).getName());
							responseString.put("subject",queueNumber.getDepName());
						}

						/* 转换为json对象 */
						JSONObject jsonObject = JSONObject.fromObject(responseString);
						OutputStream os = arg0.getResponseBody();
						String response = jsonObject.toString();
						os.write(response.getBytes());
						os.close();
					} catch (IOException ie) {
						ie.printStackTrace();
						// TODO Auto-generated catch block
					} catch (Exception e) {
						e.printStackTrace();
						// TODO: handle exception
					}
					// TODO Auto-generated method stub
				}
			}).start();
			// TODO Auto-generated method stub
		}
	}
	//12、获取预约信息
	static class GetOrderImformationHandler implements HttpHandler{
		@Override
		public void handle(HttpExchange arg0) throws IOException {
			new Thread(new Runnable() {
    			@Override
				public void run() {
					try {
						DatabaseFunc df = new DatabaseFunc();
						String queryString = arg0.getRequestURI().getQuery();
						/* 将获取到的报文体转换为键值对 */
						System.out.println(queryString);
						Map<String, String> queryStringInfo = FormData2Dic.formData2Dic(queryString);
						arg0.sendResponseHeaders(200, 0);
						Order order = df.findOrder(queryStringInfo.get("usrID"));
						Map<String, String> responseString = new HashMap<>();
						if (order == null)
							responseString.put("stat","0");
						else if(order.getClass().getName().equals("com.databaseManager.ProOrder")) {
							responseString.put("stat", "1");
							responseString.put("subject",((ProOrder)order).getDepName());
							responseString.put("proName",(df.findDoctor(((ProOrder)order).getEid())).getName());
						}
						else if(order.getClass().getName().equals("com.databaseManager.DepOrder")) {
							responseString.put("stat", "2");
							responseString.put("subject",((DepOrder)order).getDepName());
						}
						JSONObject jsonObject = JSONObject.fromObject(responseString);
						OutputStream os = arg0.getResponseBody();
						String response = jsonObject.toString();
						os.write(response.getBytes());
						os.close();
					} catch (IOException ie) {
						ie.printStackTrace();
						// TODO Auto-generated catch block
					} catch (Exception e) {
						e.printStackTrace();
						// TODO: handle exception
					}
					// TODO Auto-generated method stub
				}
			}).start();
			// TODO Auto-generated method stub
		}


	}
	//13、获取排队信息
	static class GetQueueImformationHandler implements HttpHandler{
		@Override
		public void handle(HttpExchange arg0) throws IOException {
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						Random random = new Random();
						QueueHandle queueHandle = new QueueHandle();
						DatabaseFunc df = new DatabaseFunc();
						String queryString = arg0.getRequestURI().getQuery();
						/* 将获取到的报文体转换为键值对 */
						Map<String, String> queryStringInfo = FormData2Dic.formData2Dic(queryString);
						Patient patient = df.findPatient(queryStringInfo.get("usrID"));
						arg0.sendResponseHeaders(200, 0);
						Map<String, String> responseString = new HashMap<>();
						QueueNumber queueNumber = df.findQueueNumber(Long.valueOf(queueHandle.getsid(patient)));
						responseString.put("subject",queueNumber.getDepName());
						responseString.put("proName",df.findDoctor(queueNumber.getEid()).getName());
						responseString.put("queueNum",queueHandle.getsid(patient));
						responseString.put("queueTotal",String.valueOf(random.nextInt(5)));
						responseString.put("stat","1");
						/* 转换为json对象 */
						JSONObject jsonObject = JSONObject.fromObject(responseString);
						OutputStream os = arg0.getResponseBody();
						String response = jsonObject.toString();
						os.write(response.getBytes());
						os.close();
					} catch (IOException ie) {
						ie.printStackTrace();
						// TODO Auto-generated catch block
					} catch (Exception e) {
						e.printStackTrace();
						// TODO: handle exception
					}
					// TODO Auto-generated method stub
				}
			}).start();
			// TODO Auto-generated method stub
		}


	}
	//14、叫号
	static class CallNumerHandler implements HttpHandler{
		@Override
		public void handle(HttpExchange arg0) throws IOException {
			new Thread(new Runnable() {

				@Override
				public void run() {
					try {
						DatabaseFunc df = new DatabaseFunc();
						QueueHandle queueHandle = new QueueHandle();
						String queryString = arg0.getRequestURI().getQuery();
						/* 将获取到的报文体转换为键值对 */
						Map<String, String> queryStringInfo = FormData2Dic.formData2Dic(queryString);
						arg0.sendResponseHeaders(200, 0);
						Map<String, String> responseString = new HashMap<>();
						if(queueHandle.IsEmptypriorityDepQueue()) {
							Patient nowPatient = queueHandle.pollpriorityDepQueue();
							if(nowPatient.getUid() == queryStringInfo.get("usrID"))
								responseString.put("stat","2");
							else
								responseString.put("stat","0");
						}
						if(queueHandle.IsEmptypriorityProQueue()){
							Patient nowPatient = queueHandle.pollpriorityProQueue();
							if(nowPatient.getUid() == queryStringInfo.get("usrID"))
								responseString.put("stat","1");
							else
								responseString.put("stat","0");
						}
						if(!queueHandle.IsEmptypriorityProQueue()){
							Patient nowPatient = queueHandle.pollnormalProQueue();
							if(nowPatient.getUid() == queryStringInfo.get("usrID"))
								responseString.put("stat","2");
							else
								responseString.put("stat","0");
						}
						if(!queueHandle.IsEmptypriorityDepQueue()){
							Patient nowPatient = queueHandle.pollnormalDepQueue();
							if(nowPatient.getUid() == queryStringInfo.get("usrID"))
								responseString.put("stat","1");
							else
								responseString.put("stat","0");
						}
						responseString.put("depRoom","1");

						JSONObject jsonObject = JSONObject.fromObject(responseString);
						OutputStream os = arg0.getResponseBody();
						String response = jsonObject.toString();
						os.write(response.getBytes());
						os.close();
					} catch (IOException ie) {
						ie.printStackTrace();
						// TODO Auto-generated catch block
					} catch (Exception e) {
						e.printStackTrace();
						// TODO: handle exception
					}
					// TODO Auto-generated method stub
				}
			}).start();
			// TODO Auto-generated method stub
		}


	}
}
